package hotel.controller.customer;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import java.util.List;

import hotel.common.Comm;
import hotel.common.MsgBox;
import hotel.dao.HotelDao;
import hotel.modal.Customer;
import hotel.modal.CustomerType;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;

public class CustomerChangeController {
	@FXML
	private Label titleLabel;
	@FXML
	private GridPane gridPane;
	@FXML
	private TextField nameField;
	@FXML
	private ChoiceBox<String> genderCh;
	@FXML
	private ComboBox<CustomerType> typeCb;
	@FXML
	private Label customerNoLbl;
	@FXML
	private Label creditLbl;
	@FXML
	private Label discountLbl;
	@FXML
	private TextField phoneField;
	@FXML
	private TextField certIdField;
	@FXML
	private Button btnOk;
	@FXML
	private Button btnCancel;
	
	private int mode = 0;


	private Customer customer;
	

	@FXML
	public void initialize() {
		initComponent();
		
		loadData();
	}
	
	private void loadData() {
		mode = 0;
		ObservableList<CustomerType> items = typeCb.getItems();
		List<CustomerType> customerType = HotelDao.getAllCustomerType();
		items.addAll(customerType);
		if(items.size() > 0) typeCb.getSelectionModel().select(0);
		genderCh.getSelectionModel().select(0);
		
	}
	
	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		String name = nameField.getText();
		String certId = certIdField.getText().toUpperCase();
		if(name.equals("") || certId.equals("")) {
			MsgBox.showWarningDialog("警告", null, "姓名和证件号码不允许为空！");
			return ;
		}
		customer.setName(name);
		customer.setCertId(certId);
		customer.setGender(genderCh.getSelectionModel().getSelectedItem());
		customer.setPhone(phoneField.getText());
		customer.setCustomerTypeId(typeCb.getSelectionModel().getSelectedItem().getCustomerTypeId());
		customer.setModified(true);
		closeWindow();
	}
	
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancelAction(ActionEvent event) {
		closeWindow();
	}
	
	public Customer getCustomer() {
		return customer;
	}

	
	public void setCustomer(Customer customer) {
		this.customer = customer;
		customer.setModified(false);
		if(customer != null) {
			customerNoLbl.setText(String.valueOf(customer.getCustomerNo()));
			creditLbl.setText(String.valueOf(customer.getCredit()));
			nameField.setText(customer.getName());
			if(customer.getGender().equals("M") || customer.getGender().equals("男")) {
				genderCh.getSelectionModel().select("男");
			} else {
				genderCh.getSelectionModel().select("女");
			}
			certIdField.setText(customer.getCertId());
			phoneField.setText(customer.getPhone());
			typeCb.getSelectionModel().select(findTypeCbIndex(customer));
		}
	}
	
	private int findTypeCbIndex(CustomerType ct) {
		if(ct == null) return 0;
		ObservableList<CustomerType> items = typeCb.getItems();
		CustomerType t = null;
		for(int i = 0; i < items.size(); i++) {
			t = items.get(i);
			if(t!= null && ct.getCustomerTypeId() == t.getCustomerTypeId()) 
				return i;
		}
		return 0;
	}
	
	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
		if(mode == 1) { // 只能查看
			titleLabel.setText("客户信息详情");
			phoneField.setEditable(false);
			certIdField.setEditable(false);
			nameField.setEditable(false);
			btnOk.setDisable(true);
		}else if(mode == 2) { // 新增
			titleLabel.setText("新增客户");
			gridPane.getRowConstraints().get(0).setMaxHeight(0); // 隐藏GridPane的第一行
		}
	}
	
	private void closeWindow() {
		Stage stage = (Stage)btnCancel.getScene().getWindow();
		stage.close();
	}
	
	private void initComponent() {
		typeCb.setCellFactory(new Callback<ListView<CustomerType>, ListCell<CustomerType>>() {
			@Override
			public ListCell<CustomerType> call(ListView<CustomerType> param) {
				ListCell<CustomerType> cell = new ListCell<CustomerType>() {
					@Override
					protected void updateItem(CustomerType item, boolean empty) {
						super.updateItem(item, empty);
						if (empty) {
							setText("");
						} else {
							setText(item.getTypeName());
						}
					}
				};
				return cell;
			}
		});
		
		typeCb.setButtonCell(new ListCell<CustomerType>() {
			@Override
			protected void updateItem(CustomerType item, boolean empty) {
				super.updateItem(item, empty);
				if (empty) {
					setText("");
				} else {
					setText(item.getTypeName());
				}
			}
		});
		
		typeCb.valueProperty().addListener(new ChangeListener<CustomerType>() {
			@Override
			public void changed(ObservableValue<? extends CustomerType> observable, CustomerType oldValue,
					CustomerType newValue) {
				if(newValue != null) {
					discountLbl.setText(String.valueOf(newValue.getDiscountRate()));
				}else {
					discountLbl.setText("1.0");
				}
			}
		});
		
		phoneField.textProperty().addListener(Comm.getNumberTextFieldListener(phoneField)); // 设置只能输入数字
	}
	
}
